package com.chensf.studydemo.guide.animation

import android.animation.Animator
import android.animation.ValueAnimator
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.activity_value_anim1.*

class ValueAnim1 : AppCompatActivity() {
    private var sendOriginWidth: Int = 0
    private var sendOriginHeight: Int = 0
    private var moreBtnWidth: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_value_anim1)
        startAnim.setOnClickListener {
            startValueAnim()
        }
        sendBtn.post {
            sendOriginWidth = sendBtn.width
            sendOriginHeight = sendBtn.height
            sendBtn.visibility = View.GONE
        }

        more.post {
            moreBtnWidth = more.width
        }
    }

    private fun startValueAnim() {
        sendBtn.visibility = View.VISIBLE
        val originWidth = sendOriginWidth
        val originHeight = sendOriginHeight
        val params = sendBtn.layoutParams
        val animator = ValueAnimator.ofFloat(0f, 1f)
        animator.duration = 200
        animator.repeatCount = 0
        animator.repeatMode = ValueAnimator.RESTART

        animator.addUpdateListener {
            val value = it.animatedValue as Float
            Log.d("foo", "value: $value")
            val currWidth = originWidth * value
            val currHeight = originHeight * value

            params.width = currWidth.toInt()
            params.height = currHeight.toInt()
            sendBtn.layoutParams = params
            sendBtn.alpha = value
        }

        animator.addListener(object : Animator.AnimatorListener{
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }

        })

        animator.start()
    }
}